﻿//
//jGridControl
//
(function ($) {
    $.fn.jGridControl = function (options) {
        //alert(this.attr('json_data'));
        var ranIndex = new Date().valueOf();
        var $this = this;
        dialogResult = {};
        if ($this.attr('readonly') != 'True') {
            $this.find('.toolBar').append('<li><a class="add" href="javascript:void(0)"><span>添加</span></a></li>');
            $this.find('.toolBar').append('<li><a class="delete" href="javascript:void(0)"><span>删除勾选</span></a></li>');
            $this.find('.toolBar>li:eq(0)').click(function () {
                var defObj = {};
                var $table = $this.find('table.list');
                $table.find('thead>tr>th[prop]').each(function () {
                    if ($(this).attr('default')) {
                        defObj[$(this).attr('prop')] = $(this).attr('default');
                    }
                });
                var url = $this.attr('editor')
                if (url.indexOf("?") > 0) {
                    $.pdialog.open($this.attr('editor') + '&object=' + $.toJSON(defObj), "dlg" + ranIndex, "添加记录", { mask: true, width: $this.attr('editor_w'), height: $this.attr('editor_h'),
                        close: function (param) {
                            insertRow(dialogResult);
                            return true;
                        }
                    });
                }
                else {
                    $.pdialog.open($this.attr('editor') + '?object=' + $.toJSON(defObj), "dlg" + ranIndex, "添加记录", { mask: true, width: $this.attr('editor_w'), height: $this.attr('editor_h'),
                        close: function (param) {
                            insertRow(dialogResult);
                            return true;
                        }
                    });
                }
            });
        }

        var groupIndex = 'group' + ranIndex;
        $this.find('table.list>thead>tr>th>input.checkboxCtrl').attr('group', groupIndex);

        $this.find('.toolBar>li:eq(1)').click(function () {
            alertMsg.confirm("确认要删除么?", {
                okCall: function () {
                    var $table = $this.find('table.list');
                    if ($this.attr('onremove') != '') {
                        var ids = [];
                        $table.find("tbody>tr>td>input:checked[name='" + groupIndex + "']").each(function () {
                            ids.push($(this).attr('value'));
                        });
                        eval($this.attr('onremove'))(ids);
                    }
                    else {
                        $table.find("tbody>tr>td>input:checked[name='" + groupIndex + "']").each(function () {
                            $table.find("tbody>tr[key='" + $(this).attr('value') + "']").remove();
                        });
                        updateValue();
                    }
                }
            });
        });


        //插入行
        var insertRow = function (result) {
            if (result[$this.attr('key_col')]) {

                var $table = $this.find('table.list');

                var $row = $table.find('tr[key=' + result[$this.attr('key_col')] + ']');

                var row = '<tr key="' + result[$this.attr('key_col')] + '">';
                row += '<td style="height:30px"><input name="' + groupIndex + '" value="' + result[$this.attr('key_col')] + '" type="checkbox"/></td>';
                $table.find('thead>tr>th[prop]').each(function () {
                    row += '<td prop="' + $(this).attr('prop') + '" style="' + $(this).attr('style') + '">' + result[$(this).attr('prop')] + '</td>';
                });
                //操作
                row += '<td align=center>'
                var opt = $this.attr('option_ext');
                if (opt && opt.length > 0) {
                    row += opt.format(result) + '&nbsp;&nbsp;';
                }
                if ($this.attr('readonly') != 'True') {
                    row += '<a edit="1" href="javascript:void(0)">编辑</a>&nbsp;&nbsp;<a del="1" href="javascript:void(0)">删除</a>';
                }
                row += '</td></tr>';
                if ($row.length > 0) {
                    $row.before(row);
                    $row.remove();
                } else {
                    $table.find('tbody>tr:last').before(row);
                }
                $table.find('tbody>tr').removeClass();
                $table.cssTable();

                $row = $table.find('tr[key=' + result[$this.attr('key_col')] + ']');
                $row.find('a[edit]').click(function () {
                    var url = $this.attr('editor')
                    if (url.indexOf("?") > 0) {
                        $.pdialog.open(url + '&object=' + encodeURIComponent($.toJSON(result)) + "&key=" + result[$this.attr('key_col')], "dlg" + ranIndex, "修改记录", { mask: true, width: $this.attr('editor_w'), height: $this.attr('editor_h'),
                            close: function (param) {
                                insertRow(dialogResult);
                                return true;
                            }
                        });
                    } else {
                        $.pdialog.open(url + '?object=' + encodeURIComponent($.toJSON(result)) + "&key=" + result[$this.attr('key_col')], "dlg" + ranIndex, "修改记录", { mask: true, width: $this.attr('editor_w'), height: $this.attr('editor_h'),
                            close: function (param) {
                                insertRow(dialogResult);
                                return true;
                            }
                        });
                    }

                });
                $row.find('a[del]').click(function () {
                    alertMsg.confirm("确认要删除?", {
                        okCall: function () {
                            if ($this.attr('onremove') != '') {
                                var ids = [result[$this.attr('key_col')]];
                                eval($this.attr('onremove'))(ids);
                            } else {
                                $row.remove();
                                updateValue();
                            }
                        }
                    });
                });
                updateValue();
            }
        }

        //更新json值
        var updateValue = function () {
            var $table = $this.find('table.list');
            var resultList = [];
            $table.find('tr[key]').each(function () {
                var $row = $(this);
                var obj = {};
                $table.find('thead>tr>th[prop]').each(function () {
                    obj[$(this).attr('prop')] = $row.find('td[prop="' + $(this).attr('prop') + '"]').html();
                });
                resultList.push(obj);
            });
            $this.find('.grid_json').val($.toJSON(resultList));
        }

        //插入初始值
        var initList = [];
        try {
            initList = $.parseJSON($this.attr('json_data'));
        } catch (e) { }
        $(initList).each(function () {
            insertRow(this);
        });
    }
})(jQuery);
